package net.lzzy.controller;


import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@Controller
public class fileController {
    private static  final String dirPath ="D:/file/";

    @GetMapping("/upload")
    public String upload(){
        return "upload";
    }


    @GetMapping ("/download")
    public String download(Model model){
        File dir=new File(dirPath);
        if(dir.exists() && dir.list().length>0){
            String[] files = dir.list();
            model.addAttribute("files", files);
        }
        else {
            model.addAttribute("msg","无需下载文件");
        }
        return "download";
    }


//    @GetMapping("/downloadfile")
//    public ResponseEntity<byte[]>downloadFile(String filename) throws IOException {
//        String dirPath="D:/file/";
//        File file = new File(dirPath+File.separator+filename);
//        HttpHeaders httpHeaders = new HttpHeaders();
//        httpHeaders.setContentDispositionFormData("attachment", URLEncoder.encode(filename,"UTF-8"));
//        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),httpHeaders, HttpStatus.OK);
//    }

//    @GetMapping("/downloadfile")
//    public byte[] downloadFile(String filename) throws IOException{
//        String dirPath="D:/file/";
//        File file= new File(dirPath+File.separator+filename);
//        return FileUtils.readFileToByteArray(file);
//    }
//    @GetMapping("/downloadfile")
//    public ResponseEntity<byte[]>downloadFile(@RequestParam("filename") String filename) throws UnsupportedEncodingException {
//        File file =new File(dirPath + File.separator + filename);
//        HttpHeaders httpHeaders=new HttpHeaders();
//        httpHeaders.setContentDispositionFormData("attachoment",URLEncoder.encode(filename,"UTF-8"));
//        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//        try{
//            return new ResponseEntity<byte[]>(FileUtils.readFileTobyteArray(file),httpHeaders,HttpStatus.OK);
//        } catch (IOException e){
//            e.printStackTrace();
//            return new ResponseEntity<byte[]>(e.getMessage().getBytes(),HttpStatus.EXPECTATION_FAILED);
//        }
//    }



    @PostMapping("/upload")
    public String uploadFile(@RequestPart("file") MultipartFile file) throws IOException {
//        if(!file.isEmpty()){
        String dirPath="D:/file/";
        String fileName =file.getOriginalFilename();
        File file1 =new File(dirPath+fileName);
        file.transferTo(file1);
        return "upload";
//        }else{
//            model.addAttribute(attributeName:"msg",attributeValue"上传文件为空");
//            return "load";
//        }

    }


}
